# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 生产者代码（发送消息）.py
# @Author: dongguangwen
# @Date  : 2025-04-12 18:38
import pika

credentials = pika.PlainCredentials('admin', 'password')
connection = pika.BlockingConnection(
    pika.ConnectionParameters('192.168.1.119', credentials=credentials, virtual_host='my_vhost'))
channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

for i in range(10):
    msg = f"任务 {i}"
    channel.basic_publish(
        exchange='',
        routing_key='task_queue',
        body=msg,
        properties=pika.BasicProperties(delivery_mode=2)  # 使消息持久化
    )
    print(f"[✔] 发送消息: {msg}")

connection.close()
